package net.ikenna.yadet.springjdbc;

import net.ikenna.yadet.core.*;
import net.ikenna.yadet.core.data.*;
import org.apache.log4j.*;
import org.springframework.jdbc.core.*;

import java.util.*;


public class SpringJdbcTemplateExtractor implements Extractor {
    private JdbcTemplate jdbcTemplate;
    private final ListDataLogger dataLogger;
    private Logger logger = Logger.getLogger(SpringJdbcTemplateExtractor.class);

    public SpringJdbcTemplateExtractor(JdbcTemplate jdbcTemplate, ListDataLogger dataLogger) {
        this.jdbcTemplate = jdbcTemplate;
        this.dataLogger = dataLogger;
    }

    public ListRowSet extract() {
        String sql = "SELECT * FROM cds";
        logger.info("[Extraction sql] " + sql);
        List<Map> result = jdbcTemplate.queryForList(sql, new Object[]{});
        ListRowSet rowSet = ListRowSet.from(result);
        dataLogger.log("[Printing extracted Data]", rowSet, logger);
        return rowSet;
    }
}
